
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.RequestPackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.UrlWrapper;

import org.apache.http.HttpEntity;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Set;

/**
 * 描述:请求数据包预处理基类（支持复合请求）
 *
 * @author jerryliu
 * @since 2015年10月22日 下午5:32:13
 */
public class HttpBaseRequestPackage implements RequestPackage {
    protected ArrayList<HashMap<String, Object>> mRequestParams;

    @Override
    public String getGetRequestParams() {
        Context context = SDKControler.getContext();
        StringBuilder getParams = new StringBuilder();
        /*--公共参数--*/
        // 固定参数
        try {
            getParams.append("?r=main");
            // merchantid
            getParams.append("&merchantid=" + SDKControler.getMerchantId());
            // appid
            getParams.append("&appid=" + SDKControler.getAppId());
            // gameid
            getParams.append("&gameid=" + SDKControler.getGameId());
            // serverid
            getParams.append("&serverid=" + SDKControler.getServerId());
            // imei
            String imei = AppUtil.getMachineSN(context);
            getParams.append("&imei=" + UrlEncodeUtil.encode(imei, "UTF-8"));
            // 渠道号
            getParams.append("&channelid=" + SDKControler.getChannelId());
            // 平台，1为android，2为ios官方版，3为ios越狱版
            getParams.append("&platform=" + 1);
            // 手机型号
            getParams.append("&model=" + UrlEncodeUtil.encode(AppUtil.getPhoneModel(), "UTF-8"));
            // 网络类型，1=wifi，2=2g，3=3g，4=4g,5=未知
            getParams.append("&nettype=" + AppUtil.getNetworkType(context));
            // uuid
            getParams.append("&uuid="
                    + UrlEncodeUtil.encode(AppUtil.getKugouUUID(context), "UTF-8"));
            // mid
            getParams.append("&mid=" + UrlEncodeUtil.encode(AppUtil.getKugouMID(context), "UTF-8"));
            // SDK版本名称
            getParams.append("&clientversionname="
                    + UrlEncodeUtil.encode(SDKControler.getSDKVersionName(), "UTF-8"));
            // SDK版本号
            int sdkVersionCode = SDKControler.getSDKVersionCode();
            getParams.append("&clientversioncode=" + sdkVersionCode);
            // 游戏版本名称
            getParams.append("&appversion="
                    + UrlEncodeUtil.encode(AppUtil.getVersionName(context), "UTF-8"));
            // 游戏版本号
            getParams.append("&appversioncode=" + AppUtil.getVersionCode(context));
            // 客户端类型(0:游戏sdk，1:hi游sdk，2:h5sdk)
            getParams.append("&clienttype=" + 0);
            // sdk类型，0:网游、1:单机
            getParams.append("&sdktype=" + SDKControler.getGameType());
            // 系统版本
            getParams.append("&sysversion="
                    + UrlEncodeUtil.encode(AppUtil.getSysVersionName(), "UTF-8"));
            User user = UserManager.getInstance().getCurrentUserInternal();
            // Sdk服务端生成verifytoken，用户游戏对接服务端之间的登录验证
            getParams.append("&verifytoken="
                    + UrlEncodeUtil.encode((user == null ? "" : user.getToken()), "UTF-8"));
            // 酷狗用户登录令牌
            if (TextUtils.isEmpty(kgToken)) {
                kgToken = user == null ? "" : user.getKugouToken();
            }
            getParams.append("&kgtoken=" + UrlEncodeUtil.encode(kgToken, "UTF-8"));
            // 来源 1 酷狗SDK 2 游戏中心
            getParams.append("&sourcetype=" + 1);
            // 用户名
            getParams.append("&username=" + UrlEncodeUtil.encode(username, "UTF-8"));

            /*--每个接口特有参数--*/
            if (mRequestParams != null && mRequestParams.size() >= 0) {
                int size = mRequestParams.size();
                JSONArray jsonArray = new JSONArray();
                JSONObject jsonObj;

                for (int i = 0; i < size; i++) {
                    HashMap<String, Object> cmdParams = mRequestParams.get(i);
                    jsonObj = new JSONObject();
                    final Set<String> keys = cmdParams.keySet();
                    for (String key : keys) {
                        jsonObj.put(key, cmdParams.get(key));
                    }
                    jsonArray.put(jsonObj);
                }

                String jsonStr = jsonArray.toString();
                getParams.append("&jsonparam=" + UrlEncodeUtil.encode(jsonStr, "UTF-8"));
                getParams.append("&sign="
                        + AppUtil.getSign(SDKControler.getMerchantId(), SDKControler.getAppId(),
                                SDKControler.getAppKey(), username, kgToken, imei, sdkVersionCode));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String str = getParams.toString();
        LogUtil.d("request", UrlWrapper.SERVER_HOST + str);
        return str;
    }

    private String username = "";

    /** 为公共字段username赋值 */
    public void setUserName(String userName) {
        this.username = userName;
    }

    private String kgToken = "";

    /** 为公共字段kgToken赋值 */
    public void setKgToken(String kgToken) {
        this.kgToken = kgToken;
    }

    public ArrayList<HashMap<String, Object>> getRequestParams() {
        return mRequestParams;
    }

    public void setRequestParams(ArrayList<HashMap<String, Object>> requestList) {
        this.mRequestParams = requestList;
    }

    @Override
    public Hashtable<String, String> getRequestHeaders() {
        return null;
    }

    @Override
    public Hashtable<String, Object> getSettings() {
        Hashtable<String, Object> settings = new Hashtable<String, Object>();
        // 设置3分钟的请求超时
        settings.put("conn-timeout", 3 * 60 * 1000);
        settings.put("socket-timeout", 3 * 60 * 1000);
        return settings;
    }

    @Override
    public HttpEntity getPostRequestEntity() {
        return null;
    }

    @Override
    public int getRequestType() {
        return RequestPackage.TYPE_GET;
    }

    @Override
    public String getUrl() {
        return UrlWrapper.SERVER_HOST;
    }
}
